<Project>
  <!-- This depends on the root Directory.Build.targets imported this file -->
  <UsingTask TaskName="MonoAOTCompiler" AssemblyFile="$(MonoAOTCompilerTasksAssemblyPath)" />
  <UsingTask TaskName="ILStrip" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />
  <UsingTask TaskName="RuntimeConfigParserTask" AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

  <!-- TODO: this breaks runtime tests on Helix due to the file not being there for some reason. Once this is fixed we can remove the UpdateRuntimePack target here -->
  <!--<Import Project="$(RepositoryEngineeringDir)targetingpacks.targets" Condition="'$(TargetingpacksTargetsImported)' != 'true'"/>-->
  <Import Project="$(MSBuildThisFileDirectory)WasiApp.targets" />

  <!-- FIXME: use proper dependency -->
  <Target Name="SetupAppHostConfig" BeforeTargets="_WasmGenerateAppBundle">
    <PropertyGroup>
      <_WasmMainJSFileName>$([System.IO.Path]::GetFileName('$(WasmMainJSPath)'))</_WasmMainJSFileName>
    </PropertyGroup>
    <ItemGroup>
        <HostConfig Include="v8" Host="v8" js-path="$(_WasmMainJSFileName)" />
        <HostConfig Include="node" Host="nodejs" js-path="$(_WasmMainJSFileName)" />
        <HostConfig Include="browser" Host="browser" html-path="index.html" />
    </ItemGroup>
  </Target>

  <!-- Use local runtime pack -->
  <Target Name="UpdateRuntimePack" AfterTargets="ResolveFrameworkReferences">
    <PropertyGroup>
      <_LocalMicrosoftNetCoreAppRuntimePackDir>$([MSBuild]::NormalizeDirectory($(ArtifactsBinDir), 'microsoft.netcore.app.runtime.wasi-wasm', $(Configuration)))</_LocalMicrosoftNetCoreAppRuntimePackDir>
    </PropertyGroup>
    <ItemGroup>
      <ResolvedRuntimePack PackageDirectory="$(_LocalMicrosoftNetCoreAppRuntimePackDir)"
                           Condition="'$(_LocalMicrosoftNetCoreAppRuntimePackDir)' != '' and
                                      '%(ResolvedRuntimePack.FrameworkName)' == 'Microsoft.NETCore.App'" />
    </ItemGroup>
    <Message Text="Used runtime pack: %(ResolvedRuntimePack.PackageDirectory) for $(MSBuildProjectName)" Importance="normal" />
  </Target>

  <Target Name="RebuildWasiAppBuilder">
    <ItemGroup>
      <WasiAppBuildProject Include="$([MSBuild]::NormalizePath($(RepoTasksDir), 'WasiAppBuilder', 'WasiAppBuilder.csproj'))" />
    </ItemGroup>

    <MSBuild Projects="@(WasiAppBuildProject)"
         Properties="Configuration=Debug;MSBuildRestoreSessionId=$([System.Guid]::NewGuid())"
         Targets="Restore"/>

    <MSBuild Projects="@(WasiAppBuildProject)"
         Properties="Configuration=Debug"
         Targets="Build"/>
  </Target>

  <Target Name="CopyAppZipToHelixTestDir"
          Condition="'$(WasmCopyAppZipToHelixTestDir)' == 'true' and '$(WasmBuildingForNestedPublish)' != 'true'"
          AfterTargets="Build"
          DependsOnTargets="WasmTriggerPublishApp">
    <PropertyGroup>
      <WasmHelixTestAppRelativeDir Condition="'$(WasmHelixTestAppRelativeDir)' == ''">$(MSBuildProjectName)</WasmHelixTestAppRelativeDir>
      <!-- Helix properties -->
      <!-- AnyCPU as Platform-->
      <OSPlatformConfig>$(TargetOS).AnyCPU.$(Configuration)</OSPlatformConfig>
      <HelixArchiveRoot>$([MSBuild]::NormalizeDirectory($(ArtifactsDir), 'helix'))</HelixArchiveRoot>
      <HelixArchiveRunOnlyRoot>$([MSBuild]::NormalizeDirectory($(HelixArchiveRoot), 'runonly'))</HelixArchiveRunOnlyRoot>
      <HelixArchiveRunOnlyAppsDir>$([MSBuild]::NormalizeDirectory($(HelixArchiveRunOnlyRoot), $(OSPlatformConfig), $(WasmHelixTestAppRelativeDir)))</HelixArchiveRunOnlyAppsDir>
      <ZippedApp>$(OutputPath)$(AssemblyName).zip</ZippedApp>
    </PropertyGroup>
    <ZipDirectory SourceDirectory="$(WasiAppDir)" DestinationFile="$(ZippedApp)" />
    <Copy SourceFiles="$(ZippedApp)" DestinationFolder="$(HelixArchiveRunOnlyAppsDir)" />
  </Target>
</Project>
